import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:leader_help/page/home/key_point/widgets/custom_search.dart';
import 'package:leader_help/page/home/main/widgets/widgets.dart';
import 'package:leader_help/utils/component/component.dart';
import 'package:leader_help/utils/icon_util.dart';
import 'package:leader_help/utils/img_util.dart';
import 'package:leader_help/value/topcolors.dart';

import 'key_think_tank_logic.dart';

class KeyThinkTankPage extends StatefulWidget {
  const KeyThinkTankPage({Key? key}) : super(key: key);

  @override
  _KeyThinkTankPageState createState() => _KeyThinkTankPageState();
}

class _KeyThinkTankPageState extends State<KeyThinkTankPage> {
  final logic = Get.put(KeyThinkTankLogic());
  final state = Get.find<KeyThinkTankLogic>().state;

  late List<Widget> widgets = [
    const RecommendListItem(),
    const ThinkTankPointListItem(),
    const ThinkTankStudyListItem(),
    const Center()
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: detailAppBar(
        context,
        title: "国家智库",
        actions: [
          Container(
            margin: EdgeInsets.only(right: 10.r),
            child: InkWell(
              child: Row(
                children: [
                  const Icon(
                    ProIconUtil.entrust,
                    size: 18,
                    color: Colors.white,
                  ),
                  SizedBox(
                    width: 2.r,
                  ),
                  const Text(
                    "课题委托",
                    style: TextStyle(color: Colors.white, fontSize: 15),
                  ),
                ],
              ),
              onTap: () {},
            ),
          ),
        ],
      ),
      body: Column(
        children: [
          Container(
            padding: EdgeInsets.only(top: 5.r, bottom: 5.r),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(2.r),
            ),
            child: const CustomSearch(
              hintLabel: "请输入搜索关键词",
            ),
          ),
          Material(
            color: Colors.white,
            child: TabBar(
              tabs: [
                for (final tab in state.tabs)
                  Tab(
                    text: tab,
                  )
              ],
              indicatorColor: TopColors.main_red,
              indicator: BoxDecoration(
                  borderRadius: BorderRadius.circular(2),
                  color: TopColors.main_red),
              indicatorPadding: const EdgeInsets.only(
                  left: 20, right: 20, top: 40, bottom: 5),
              // 字体颜色
              labelColor: TopColors.main_red,
              labelStyle:
                  const TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
              labelPadding: const EdgeInsets.only(left: 13, right: 13),
              // 没有选中字体颜色
              unselectedLabelColor: TopColors.black_33,
              unselectedLabelStyle: const TextStyle(fontSize: 15),
              // 是否滚动
              isScrollable: false,
              // 控制器
              controller: logic.controller,
              physics: const BouncingScrollPhysics(),
            ),
          ),
          Expanded(
            child: TabBarView(controller: logic.controller, children: widgets),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<KeyThinkTankLogic>();
    super.dispose();
  }
}

/// 推荐
class RecommendListItem extends GetView<KeyThinkTankLogic> {
  const RecommendListItem({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        padding: EdgeInsets.zero,
        itemCount: controller.state.type.length,
        itemBuilder: (BuildContext context, int index) {
          return controller.state.type[index] == 0
              ? ThinkTankPointItem(controller, index)
              : ThinkTankStudyItem(controller, index);
        });
  }
}

/// 专家观点
class ThinkTankPointListItem extends GetView<KeyThinkTankLogic> {
  const ThinkTankPointListItem({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        padding: EdgeInsets.zero,
        itemCount: controller.state.name.length,
        itemBuilder: (BuildContext context, int index) {
          return ThinkTankPointItem(controller, index);
        });
  }
}

/// 专家观点 Item
Widget ThinkTankPointItem(KeyThinkTankLogic controller, int index) {
  return Container(
    margin: EdgeInsets.only(left: 5.r, right: 5.r, top: 8.r, bottom: 8.r),
    padding: EdgeInsets.only(left: 8.r, right: 8.r, top: 10.r, bottom: 10.r),
    decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5.r), color: Colors.white),
    child: Column(
      children: [
        Row(
          children: [
            Image.asset(
              "avatar_default.png".imgPathMe,
              height: 30.r,
            ),
            SizedBox(
              width: 5.r,
            ),
            Text(
              controller.state.name[index],
              style: TextStyle(color: TopColors.black_33, fontSize: 15),
            ),
            SizedBox(
              width: 5.r,
            ),
            Flexible(
                child: Text(
              controller.state.position[index],
              style: TextStyle(color: TopColors.green_live, fontSize: 13),
            )),
          ],
        ),
        SizedBox(
          height: 5.r,
        ),
        TPText(
          text: "    " + controller.state.content[index],
          // style: TextStyle(color: TopColors.black_33, fontSize: 14),
        ),
        SizedBox(
          height: 10.r,
        ),
        RichText(
          text: TextSpan(children: [
            TextSpan(
              text: "-" + controller.state.time[index],
              style: TextStyle(
                color: TopColors.black_33,
                fontSize: 15,
              ),
            ),
            TextSpan(
              text: controller.state.source[index],
              style: const TextStyle(
                color: Color(0xff1977FF),
                fontSize: 15,
              ),
            )
          ]),
        ),
        SizedBox(
          height: 10.r,
        ),
        BottomWidget(),
      ],
    ),
  );
}

/// 智库研究
class ThinkTankStudyListItem extends GetView<KeyThinkTankLogic> {
  const ThinkTankStudyListItem({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        padding: EdgeInsets.zero,
        itemCount: controller.state.organization.length,
        itemBuilder: (BuildContext context, int index) {
          return ThinkTankStudyItem(controller, index);
        });
  }
}

///  智库研究 Item
Widget ThinkTankStudyItem(KeyThinkTankLogic controller, int index) {
  return Container(
    margin: EdgeInsets.only(left: 5.r, right: 5.r, top: 8.r, bottom: 8.r),
    padding: EdgeInsets.only(left: 8.r, right: 8.r, top: 10.r, bottom: 10.r),
    decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5.r), color: Colors.white),
    child: Column(
      children: [
        Row(
          children: [
            Image.asset(
              index == 2 ? "u772.png".imgPathTask : "u781.jpg".imgPathTask,
              height: 30.r,
            ),
            SizedBox(
              width: 5.r,
            ),
            Text(
              controller.state.organization[index],
              style: const TextStyle(color: Color(0xff0a59ac), fontSize: 18),
            ),
          ],
        ),
        SizedBox(
          height: 5.r,
        ),
        // Text("    " + controller.state.content1[index], style: TextStyle(color: TopColors.black_33, fontSize: 14),),
        TPText(text: "    " + controller.state.content1[index]),
        SizedBox(
          height: 10.r,
        ),
        RichText(
          text: TextSpan(children: [
            TextSpan(
              text: "-" + controller.state.time1[index],
              style: TextStyle(
                color: TopColors.black_33,
                fontSize: 15,
              ),
            ),
            TextSpan(
              text: controller.state.source1[index],
              style: const TextStyle(
                color: Color(0xff1977FF),
                fontSize: 15,
              ),
            )
          ]),
        ),
        SizedBox(
          height: 10.r,
        ),
        BottomWidget(),
      ],
    ),
  );
}

/// 底部通用
Widget BottomWidget() {
  Color A7A7A7 = const Color(0xffA7A7A7);
  TextStyle style = TextStyle(color: A7A7A7, fontSize: 15);
  return Container(
    padding: EdgeInsets.only(left: 5.r, right: 5.r),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Icon(
              ProIconUtil.eye,
              size: 16.r,
              color: A7A7A7,
            ),
            SizedBox(
              width: 3.r,
            ),
            Text(
              "查看原文",
              style: style,
            )
          ],
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Icon(
              ProIconUtil.forward,
              size: 16.r,
              color: A7A7A7,
            ),
            SizedBox(
              width: 3.r,
            ),
            Text(
              "下发学习",
              style: style,
            )
          ],
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Icon(
              ProIconUtil.instruct,
              size: 16.r,
              color: A7A7A7,
            ),
            SizedBox(
              width: 3.r,
            ),
            Text(
              "稍后阅读",
              style: style,
            )
          ],
        ),
      ],
    ),
  );
}
